package com.esp.soccertutor.uc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;


import com.esp.soccertutor.R;
import com.esp.soccertutor.android.fbreader.FBReader;
import com.esp.soccertutor.android.fbreader.SearchAction;
import com.esp.soccertutor.android.fbreader.TOCActivity;
import com.esp.soccertutor.fbreader.fbreader.FBReaderApp;
import com.esp.soccertutor.ui.AppHome;
import com.esp.soccertutor.zlibrary.core.application.ZLApplication;

public class CustomMenu_BottomReading {
	
	private Context mContext = null;
	private FBReader baseActivity=null;
	private Activity mActivity = null;
	private LayoutInflater mLayoutInflater = null;
	private PopupWindow mPopupWindow = null;
	private boolean mIsShowing = false;
	public ImageView imagebrightness;
	
	private LinearLayout frameMain;
	
	private FontZoom_Dialog objDialog;
	
	public boolean isShowing() { return mIsShowing; }
	private View mView;
	public CustomMenu_BottomReading(FBReader baseActivity, Activity activity, Context context, LayoutInflater lo) {
		mContext = context;
		this.baseActivity = baseActivity;
		mActivity = activity;
		mLayoutInflater = lo;
		mView= mLayoutInflater.inflate(R.layout.custom_menu_bottom_for_reading, null);
		
		frameMain = (LinearLayout) mView.findViewById(R.id.MenuFrame);
		
		imagebrightness=(ImageView)mView.findViewById(R.id.btnBrightness);
		
		this.objDialog = new FontZoom_Dialog(context, lo);
	}

	public void setBackGround(int Resource)
	{
		frameMain.setBackgroundResource(Resource);
	}

	public boolean isFontDialogShowing(){return objDialog.isShowing();}
	public void hideFontDialog(){objDialog.hide();}
	
	public synchronized void show(final View objv) {
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		//View mView= mLayoutInflater.inflate(R.layout.custom_menu_bottom_for_reading, null);
		
		int[] btn = new int[]{R.id.btnTableOfContant, R.id.btnSettings, R.id.btnSearch, R.id.btnFontSize};
		for(int ID : btn)
		{
			ImageView bt = (ImageView) mView.findViewById(ID);
			bt.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					Intent intent = null;
					switch (v.getId()) {
						case R.id.btnTableOfContant:
							intent = new Intent(mContext, TOCActivity.class);
							mActivity.startActivityForResult(intent, FBReader.REPAINT_CODE);
							break;
						case R.id.btnSettings:
							baseActivity.startActivityForResult(new Intent(baseActivity.getApplicationContext(),AppHome.class),FBReader.REPAINT_CODE);
							//intent = new Intent(mContext, GSHome.class);
							//mActivity.startActivityForResult(intent, FBReader.REPAINT_CODE);
							break;
						case R.id.btnSearch:
							FBReaderApp fbReader = (FBReaderApp)ZLApplication.Instance();
							new SearchAction(baseActivity, fbReader).run();
							break;
						case R.id.btnFontSize:
								objDialog.show(objv);
//							  intent = new Intent(mContext, AppFontSize.class);
//							  mActivity.startActivity(intent);
							break;
						default:
							break;
					}
					hide();
					FBReader.TopMenu.hide();
					
				}
			});
		}
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        mPopupWindow.setWidth(display.getWidth());
        mPopupWindow.showAtLocation(objv, Gravity.BOTTOM, 0, 0);
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}
}
